package org.ceteca.explica.server.warehouse;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import org.ceteca.explica.core.ExplicaException;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.MeasureUnitOT;
import org.ceteca.explica.core.warehouse.ProductOT;
import org.ceteca.explica.core.warehouse.WarehouseManager;
import org.ceteca.explica.server.ServerContext;
import org.ceteca.explica.server.util.db.CriteriaBean;
import org.ceteca.explica.core.util.FieldValue;
import org.ceteca.explica.server.util.db.InterfaceDB;
import org.ceteca.explica.server.warehouse.bl.ProductBLL;

import org.ceteca.explica.core.warehouse.ProductGroupOT;

public class WarehouseManagerImpl extends UnicastRemoteObject implements WarehouseManager {
	private static final long serialVersionUID = 123456789;
	public WarehouseManagerImpl() throws RemoteException {
		super();
/*		if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }

		try {
			// Se registra la clase con el servidor de registro de RMI en el host local
			// con el nombre "Almacen". Al no indicarse puerto se usará el puerto TCP 1099.
			System.out.println("Rebind del servidor!");
			Naming.rebind(InterfaceAxilice.JNDI_INSTALLER_EXPORT_PORT, this);
		}
		catch (AccessException aEx) {
			System.err.println("Se rechazó el acceso. Su sistema no tiene los permisos necesarios para realizar la operación remota!");
			aEx.printStackTrace();
			System.exit(-1);
		}
		catch (MalformedURLException mEx) {
			System.err.println("La URL para el registro no es válida!");
			mEx.printStackTrace();
			System.exit(-1);
		}
*/
	}
	
	public List queryProducts(String fieldId, String value) throws RemoteException, ExplicaException {
		ArrayList<CriteriaBean> lCriteria = new ArrayList<CriteriaBean>();

		// Add the query criteria to the list
		if (fieldId != null && fieldId.length() > 0 && value != null && value.length() > 0) {
			lCriteria.add(this.buildCriteria(fieldId, FieldValue.getFieldValue(ProductOT.class, fieldId, value)));
		}
		
		return ProductBLL.getInstance().queryProducts(lCriteria);
	}
	
	public OperationResult delProduct(Long idProduct) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delProduct(idProduct);
	}
	
	public OperationResult saveProduct(ProductOT product) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().saveProduct(product);
	}
	
	public OperationResult delProducts(ArrayList<Long> lSelProducts) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delProducts(lSelProducts);
	}
	
	public ProductOT getProduct(Long idProduct) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().getProduct(idProduct);
	}
	
	public List queryProductGroups(String fieldId, String value) throws RemoteException, ExplicaException {
		ArrayList<CriteriaBean> lCriteria = new ArrayList<CriteriaBean>();
		
		WarehouseManagerBuilder.getLogger().debug("((((((((((((((PRODUCT_GROUPS)))))))))))))))) " + lCriteria);
		
		// Add the query criteria to the list
		if (fieldId != null && fieldId.length() > 0 && value != null && value.length() > 0) {
			lCriteria.add(this.buildCriteria(fieldId, FieldValue.getFieldValue(ProductGroupOT.class, fieldId, value)));
		}
		
		return ProductBLL.getInstance().queryProductGroups(lCriteria);
	}
	
	public OperationResult delProductGroup(Long idGroup) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delProductGroup(idGroup);
	}
	
	public OperationResult saveProductGroup(ProductGroupOT group) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().saveProductGroup(group);
	}
	
	public OperationResult delProductGroups(ArrayList<Long> lSelGroups) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delProductGroups(lSelGroups);
	}
	
	public ProductGroupOT getProductGroup(Long idGroup) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().getProductGroup(idGroup);
	}
	
	public long getNextProductGroupId() throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().getNextProductGroupId();
	}

	public void resetNextProductGroupId(long idGroup) throws RemoteException, ExplicaException {
		ProductBLL.getInstance().resetNextProductGroupId(idGroup);
	}
	
	public List queryMeasureUnits(String fieldId, String value) throws RemoteException, ExplicaException {
		ArrayList<CriteriaBean> lCriteria = new ArrayList<CriteriaBean>();
		
		// Add the query criteria to the list
		if (fieldId != null && fieldId.length() > 0 && value != null && value.length() > 0) {
			lCriteria.add(this.buildCriteria(fieldId, FieldValue.getFieldValue(MeasureUnitOT.class, fieldId, value)));
		}
		
		System.out.println("[[[[[[[[[[[[]]]]]]]]]]]] " + lCriteria);
		
		return ProductBLL.getInstance().queryMeasureUnits(lCriteria);
	}
	
	public OperationResult delMeasureUnit(Long idMeasureUnit) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delMeasureUnit(idMeasureUnit);
	}
	
	public OperationResult saveMeasureUnit(MeasureUnitOT measureUnit) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().saveMeasureUnit(measureUnit);
	}
	
	public OperationResult delMeasureUnits(ArrayList<Long> lSelMeasureUnits) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().delMeasureUnits(lSelMeasureUnits);
	}
	
	public MeasureUnitOT getMeasureUnit(Long idMeasureUnit) throws RemoteException, ExplicaException {
		return ProductBLL.getInstance().getMeasureUnit(idMeasureUnit);
	}

	private CriteriaBean buildCriteria(String fieldId, Object value) {
		CriteriaBean result = null;
		Object val = null;
		String op;
		
		if (value instanceof String) {
			op = InterfaceDB.SQL_OPERATOR_LIKE;
			val = new String("%" + value + "%");
		}
		else {
			op = InterfaceDB.SQL_OPERATOR_EQUAL;
			val = value;
		}
		
		// Add the query criteria to the list
		if (fieldId != null && fieldId.length() > 0 && val != null) {
			result = new CriteriaBean(fieldId, val, op, null, null, null);
		}
		
		return result;
	}
}
